package ink.metoo.gude.module.websocket.controller

import ink.metoo.gude.module.websocket.domain.dto.HistoryMessageDTO
import ink.metoo.gude.module.websocket.domain.dto.MessageDTO
import ink.metoo.gude.module.websocket.service.MessageService
import jakarta.validation.Valid
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.handler.annotation.Payload
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.Authentication
import org.springframework.stereotype.Controller

@Controller
@MessageMapping("/message")
class MessageController(private val messageService: MessageService) {

    /**
     * 发送消息
     */
    @PreAuthorize("isAuthenticated()")
    @MessageMapping("/send")
    fun onMessage(@Payload message: MessageDTO, principal: Authentication) =
        messageService.doMessage(message, principal)


    /**
     * 获得历史消息
     */
    @MessageMapping("/history")
    fun onHistoryMessage(@Valid @Payload dto: HistoryMessageDTO, principal: Authentication) =
        messageService.doHistoryMessage(dto, principal)

}